package yt.kpool.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.MainActivity;
import yt.kpool.R;
import yt.kpool.activity.home.CalculatorAc;
import yt.kpool.activity.home.MainstreamCurrencyAc;
import yt.kpool.activity.home.PowAc;
import yt.kpool.activity.personal.FastLookActivity;
import yt.kpool.activity.personal.LoginActivity;
import yt.kpool.adapter.SimpleAdapter;
import yt.kpool.base.BaseApplication;
import yt.kpool.bean.HomeListBean;
import yt.kpool.bean.HomeTopOneBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.SPUtil;
import yt.kpool.util.ToastUtil;
import yt.kpool.util.Util;
import yt.kpool.view.HeaderProductLayout;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.lazyviewpager.LazyFragmentPagerAdapter;
import yt.kpool.view.pop.HomeSelectOnePop;
import yt.kpool.view.pop.HomeSelectThreePop;
import yt.kpool.view.pop.HomeSelectTwoPop;


/**
 * 首页
 */
public class HomePageFragment extends Fragment implements LazyFragmentPagerAdapter.Laziable{
    @BindView(R.id.ly_top)
    LinearLayout ly_top;
    @BindView(R.id.ly_top1)
    LinearLayout ly_top11;
    @BindView(R.id.ly_top2)
    LinearLayout ly_top22;
    @BindView(R.id.ly_top3)
    LinearLayout ly_top33;
    @BindView(R.id.ly_change)
    LinearLayout ly_change;//切换
    @BindView(R.id.main_recycler)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_1)
    TextView tv_1;
    @BindView(R.id.tv_2)
    TextView tv_2;
    @BindView(R.id.tv_3)
    TextView tv_3;
    @BindView(R.id.iv_1)
    ImageView iv_1;
    @BindView(R.id.iv_2)
    ImageView iv_2;
    @BindView(R.id.iv_3)
    ImageView iv_3;
    @BindView(R.id.tv_c1)
    TextView tv_c1;
    @BindView(R.id.tv_c2)
    TextView tv_c2;
    @BindView(R.id.tv_c3)
    TextView tv_c3;
    private HeaderProductLayout mHeaderView;
    private int imageY = 0;
    @BindView(R.id.swipe)
    SwipeRefreshLayout swipeRefreshLayout;
    private LinearLayout ly_top1,ly_top2,ly_top3,ly_tops,ly_changes;
    private TextView tv_11,tv_22,tv_33,tv_c1s,tv_c2s,tv_c3s;
    private boolean mShouldScroll;
    private SimpleAdapter simpleAdapter;
    private Double Exchange;
    //记录目标项位置
    private int mToPosition;
    private HomeSelectOnePop homeSelectOnePop;
    private HomeSelectTwoPop homeSelectTwoPop;
    private HomeSelectThreePop homeSelectThreePop;
    private LoadingDialog dialog;
    private  HomeListBean homeListBean;
    private  List<HomeListBean.DataBean.MinerModelListBean> listData=new ArrayList<>();
    private SPUtil spUtil;
    boolean isfresh,isdownfresh;
    private boolean isChange=true;
    private   List<HomeTopOneBean.DataBean> list_coin=new ArrayList<>();
    private String flash_p="0.27";
    private String code="";
    private int pricekind=2;
    private int kind;
    private int Dis=0;
    @BindView(R.id.image_button)
    ImageView image_button;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_homepage, container, false);
        ButterKnife.bind(this, view);
        init();
        if(NetWorkUtil.checkNet(getActivity()))
        {
            getNetData("");
            getNetCoin();
        }
        else
        {
            ToastUtil.showToast(getActivity(),getResources().getString(R.string.internet_error));
        }

        return view;
    }

    /**
     *初始化数据
     */
    private void init()
    {
        if (spUtil == null)
            spUtil = new SPUtil(getContext(), "fileName");
        if (spUtil.getInt(BaseApplication.MONEYKIND, 0) == 1)
        {
            flash_p="0.27";

            kind=1;
        }
        else if(spUtil.getInt(BaseApplication.MONEYKIND, 0) == 2)
        {
            flash_p="0.040";

            kind=2;
        }
        swipeRefreshLayout.setColorSchemeResources(R.color.system_color);
        dialog = new LoadingDialog(getActivity(), 0);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        EventBus.getDefault().register(this);
        mHeaderView = new HeaderProductLayout(getContext());
        LinearLayout ly_zlb= mHeaderView.findViewById(R.id.ly_zlb);
        LinearLayout ly_pow= mHeaderView.findViewById(R.id.ly_pow);
        LinearLayout ly_jsq= mHeaderView.findViewById(R.id.ly_jsq);
        ly_top1 = mHeaderView.findViewById(R.id.ly_top1);
        ly_top2 = mHeaderView.findViewById(R.id.ly_top2);
        ly_top3 = mHeaderView.findViewById(R.id.ly_top3);
        ly_changes= mHeaderView.findViewById(R.id.ly_change);
        tv_11= mHeaderView.findViewById(R.id.tv_1);
        tv_22= mHeaderView.findViewById(R.id.tv_2);
        tv_33= mHeaderView.findViewById(R.id.tv_3);
        tv_c1s= mHeaderView.findViewById(R.id.tv_c1);
        tv_c2s= mHeaderView.findViewById(R.id.tv_c2);
        tv_c3s= mHeaderView.findViewById(R.id.tv_c3);
        ly_tops=mHeaderView.findViewById(R.id.ly_top);
        simpleAdapter = new SimpleAdapter(getContext(),listData);
        simpleAdapter.addHeaderView(mHeaderView);
        mRecyclerView.setAdapter(simpleAdapter);
        ly_zlb.setOnClickListener(new View.OnClickListener() {//主流币
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MainstreamCurrencyAc.class));
            }
        });
        ly_pow.setOnClickListener(new View.OnClickListener() {//pow
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), PowAc.class));
            }
        });
        ly_jsq.setOnClickListener(new View.OnClickListener() {//计算器
            @Override
            public void onClick(View v) {
                CalculatorAc.start(getActivity(),"0");
            }
        });
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                int[] location = new int[2];
                ly_top.getLocationOnScreen(location);
                int y = location[1];
                imageY = y;
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (mHeaderView == null) return;
                int getTop = mHeaderView.getDistanceY();
                if (getTop <= imageY) {
                    ly_top.setVisibility(View.VISIBLE);
                } else {
                    ly_top.setY(0);
                    ly_top.setVisibility(View.GONE);
                }
            }
        });
        /**
         * 下拉刷新
         */
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                if(NetWorkUtil.checkNet(getActivity()))
                {
                    isdownfresh=true;
                    getNetData(code);
                }
                else
                {
                    swipeRefreshLayout.setRefreshing(false);
                    ToastUtil.showToast(getActivity(),getResources().getString(R.string.internet_error));
                }

                //刷新完成
            }
        });
        initPop();
        /**
         * 顶部第一个筛选条件
         */
        ly_top1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectStyle(1,true);
                smoothMoveToPosition(mRecyclerView, 1);
                new Handler().postDelayed(new Runnable(){
                    public void run(){
                        //execute the task
                        homeSelectOnePop.showAsDropDown(ly_top1, 0, 0);

                    }
                },200);



            }
        });
        /**
         * 顶部第二个筛选条件
         */
        ly_top2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectStyle(2,true);
                smoothMoveToPosition(mRecyclerView, 1);
                new Handler().postDelayed(new Runnable(){
                    public void run(){
                        //execute the task
                        homeSelectTwoPop.showAsDropDown(ly_top1, 0, 0);

                    }
                },200);
            }
        });
        /**
         * 顶部第三个筛选条件
         */
        ly_top3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectStyle(3,true);
                smoothMoveToPosition(mRecyclerView, 1);
                new Handler().postDelayed(new Runnable(){
                    public void run(){
                        //execute the task
                        homeSelectThreePop.showAsDropDown(ly_top1, 0, 0);
                    }
                },200);
            }
        });
        /**
         * 切换
         */
        ly_changes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeText();
            }
        });
        final View getHeightView = new View(getActivity());
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                getHeightView.scrollBy(0, dy);
                if(getHeightView.getScrollY()>1000)
                {
                    image_button.setVisibility(View.VISIBLE);
                }
                else
                {
                    image_button.setVisibility(View.GONE);
                }
            }
        });

    }

    /**
     * 获取首页网络数据
     */
    private void getNetData(String CurrencyCode)
    {

        if(NetWorkUtil.checkNet(getActivity()))
        {
            if(!isdownfresh)
            {
                dialog.show();
            }

            NetHelperNew.MinerModelList(CurrencyCode, new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    homeListBean = MyGson.getInstance().fromJson(data, HomeListBean.class);
                    listData=homeListBean.getData().getMinerModelList();
                    simpleAdapter.setNewData(listData);
                    BaseApplication.Exchange=homeListBean.getData().getExchange();
                    simpleAdapter.setExchange(homeListBean.getData().getExchange());
                    simpleAdapter.setKind(kind,Double.valueOf(flash_p));
                    sort(pricekind);
                    simpleAdapter.notifyDataSetChanged();

                    if(!isdownfresh)
                    {
                        dialog.dismiss();
                    }
                    if(isdownfresh)
                    {
                        swipeRefreshLayout.setRefreshing(false);
                    }
                    isdownfresh=false;
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    if(!isdownfresh)
                    {
                        dialog.dismiss();
                    }
                    if(isdownfresh)
                    {
                        swipeRefreshLayout.setRefreshing(false);
                    }
                    isdownfresh=false;
                    ToastUtil.showToast(getActivity(),msg);
                }
            });

        }
        else
        {

            ToastUtil.showToast(getActivity(),getResources().getString(R.string.internet_error));
        }


    }

    /**
     * 排序
     */
    private void sort(final int kindd)
    {

        for(int i=0;i<listData.size();i++)
        {


            BigDecimal c1 = new BigDecimal(listData.get(i).getCurrencyPrice());
            BigDecimal c2 = new BigDecimal(BaseApplication.Exchange);
            String cp1=c1.multiply(c2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"";


            BigDecimal b1 = new BigDecimal(listData.get(i).getThroughput());
            BigDecimal b2 = new BigDecimal(cp1);
            String sp1;
            if(kind==1)
            {
                 sp1=b1.multiply(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"";
            }
            else
            {
                sp1=Util.Transformation(b1.multiply(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"");
            }

            BigDecimal bb1 = new BigDecimal(listData.get(i).getPower());
            BigDecimal bb2 = new BigDecimal(Util.NMultiplication(0.024+"",flash_p));
            String sp2=bb1.multiply(bb2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"";

            BigDecimal bbb1 = new BigDecimal(sp1);
            BigDecimal bbb2 = new BigDecimal(sp2);
            String sp3=bbb1.subtract(bbb2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"";
            listData.get(i).setIncome(Double.valueOf(sp3));


        }

        Collections.sort(listData, new Comparator<HomeListBean.DataBean.MinerModelListBean>() {

            @Override
            public int compare(HomeListBean.DataBean.MinerModelListBean o1, HomeListBean.DataBean.MinerModelListBean o2) {

                if(kindd==1) // 最不赚钱的
                {
                    if (o1.getIncome() > o2.getIncome()) {
                        return 1;
                    }
                    if (o1.getIncome() == o2.getIncome()) {
                        return 0;
                    }
                    return -1;
                }
                else//最赚钱的
                {
                    if (o1.getIncome() > o2.getIncome()) {
                        return -1;
                    }
                    if (o1.getIncome() == o2.getIncome()) {
                        return 0;
                    }
                    return 1;
                }

            }
        });

    }

    /**
     * 获取币种
     */
    private void getNetCoin()
    {
        NetHelperNew.CoinDropDownModelList(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                HomeTopOneBean  homeListBean = MyGson.getInstance().fromJson(data, HomeTopOneBean.class);
                HomeTopOneBean.DataBean dataBean=new HomeTopOneBean.DataBean();
                dataBean.setCode("全部币种");
                dataBean.setName("全部币种");
                list_coin.add(dataBean);
                list_coin.addAll(homeListBean.getData());
                homeSelectOnePop.setData(list_coin);
            }
        });

    }


    /**
     * 点击事件的处理
     *
     * @param view
     */
    @OnClick({R.id.ly_top1, R.id.ly_top2, R.id.ly_top3, R.id.ly_change,R.id.iv_quick,R.id.image_button})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ly_top1:
                selectStyle(1,true);
                if(homeSelectTwoPop.isShowing())
                {
                    homeSelectTwoPop.dismiss();
                }
                if(homeSelectThreePop.isShowing())
                {
                    homeSelectThreePop.dismiss();
                }
                homeSelectOnePop.showAsDropDown(ly_top11, 0, 0);
                break;
            case R.id.ly_top2:
                selectStyle(2,true);
                if(homeSelectOnePop.isShowing())
                {
                    homeSelectOnePop.dismiss();
                }
                if(homeSelectThreePop.isShowing())
                {
                    homeSelectThreePop.dismiss();
                }
                homeSelectTwoPop.showAsDropDown(ly_top11, 0, 0);
                break;
            case R.id.ly_top3:
                selectStyle(3,true);
                if(homeSelectOnePop.isShowing())
                {
                    homeSelectOnePop.dismiss();
                }
                if(homeSelectTwoPop.isShowing())
                {
                    homeSelectTwoPop.dismiss();
                }
                homeSelectThreePop.showAsDropDown(ly_top11, 0, 0);
                break;
            case R.id.ly_change://切换
                changeText();
                break;
            case R.id.iv_quick://快速查看
                if(spUtil.getString(BaseApplication.TOKEN,"").equals(""))
                {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    intent.putExtra("type", 1);
                    startActivityForResult(intent, 50);

                }else
                {
                    startActivityForResult(new Intent(getContext(), FastLookActivity.class), 12);
                }


            break;
            case R.id.image_button:
                mRecyclerView.smoothScrollToPosition(0);
//                image_button.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 切换
     */
    private void changeText()
    {

        if(isChange)
        {
            tv_c1s.setText("算力/功率");
            tv_c2s.setText("单位功耗");
            tv_c3s.setText("关机币价");

            tv_c1.setText("算力/功率");
            tv_c2.setText("单位功耗");
            tv_c3.setText("关机币价");
            ly_change.setBackgroundResource(R.drawable.button_select_bg);
            ly_changes.setBackgroundResource(R.drawable.button_select_bg);
            isChange=false;
            simpleAdapter.setChange(2);
            simpleAdapter.notifyDataSetChanged();
        }
        else
        {
            tv_c1s.setText("日产值/日电费");
            tv_c2s.setText("当前币价");
            tv_c3s.setText("日净收益");

            tv_c1.setText("日产值/日电费");
            tv_c2.setText("当前币价");
            tv_c3.setText("日净收益");
            ly_change.setBackgroundResource(R.drawable.button_corner_bg);
            ly_changes.setBackgroundResource(R.drawable.button_corner_bg);
            isChange=true;
            simpleAdapter.setChange(1);
            simpleAdapter.notifyDataSetChanged();
        }
    }

    /**
     * RecyclerView 判断滚动距离
     * @param mRecyclerView
     * @param position
     */
    private void smoothMoveToPosition(RecyclerView mRecyclerView, final int position) {

        // 第一个可见位置
        int firstItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(0));
        // 最后一个可见位置
        int lastItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(mRecyclerView.getChildCount() - 1));
        if (position < firstItem) {
          Log.d("XX","111");
            // 第一种可能:跳转位置在第一个可见位置之前
            mRecyclerView.smoothScrollToPosition(position);
        } else if (position <= lastItem) {
            Log.d("XX","222");
            // 第二种可能:跳转位置在第一个可见位置之后
            int movePosition = position - firstItem;
            if (movePosition >= 0 && movePosition < mRecyclerView.getChildCount()) {
                int top = mRecyclerView.getChildAt(movePosition).getTop();
                if(ly_top.getHeight()==0)
                mRecyclerView.smoothScrollBy(0, top-ly_tops.getHeight());
                else
                mRecyclerView.smoothScrollBy(0, top-ly_top.getHeight());
            }
        } else {
            Log.d("XX","3333");
            // 第三种可能:跳转位置在最后可见项之后
            mRecyclerView.smoothScrollToPosition(position);
            mToPosition = position;
            mShouldScroll = true;
        }
    }

    /**
     * 初始化pop数据
     */
    private void initPop() {
        homeSelectOnePop = new HomeSelectOnePop(getActivity(),new HomeSelectOnePop.OnSelectItemListener() {
            @Override
            public void select(int i) {
//                listData.clear();
//                simpleAdapter.notifyDataSetChanged();
                if(i==0)
                {
                    getNetData("");
                    code="";
                    tv_1.setText("全部币种");
                    tv_11.setText("全部币种");
                }
                else
                {
                    code=list_coin.get(i).getCode();
                    getNetData(code);
                    tv_1.setText(list_coin.get(i).getCode().toUpperCase());
                    tv_11.setText(list_coin.get(i).getCode().toUpperCase());
                }

            }
        });
        homeSelectTwoPop = new HomeSelectTwoPop(getActivity(), new HomeSelectTwoPop.OnSelectItemListener() {
            @Override
            public void select(int i,String name) {
                 flash_p=name;
                if (spUtil.getInt(BaseApplication.MONEYKIND, 0) == 1)
                {
                    tv_2.setText(name+"¥/度");
                    tv_22.setText(name+"¥/度");
                }
                else if(spUtil.getInt(BaseApplication.MONEYKIND, 0) == 2)
                {
                    tv_2.setText(name+"$/度");
                    tv_22.setText(name+"$/度");
                }
                if (spUtil.getInt(BaseApplication.MONEYKIND, 0) == 1)
                {
                    simpleAdapter.setKind(1,Double.valueOf(name));
                    simpleAdapter.notifyDataSetChanged();
                }
                else if(spUtil.getInt(BaseApplication.MONEYKIND, 0) == 2)
                {
                    simpleAdapter.setKind(2,Double.valueOf(name));
                    simpleAdapter.notifyDataSetChanged();
                }

                smoothMoveToPosition(mRecyclerView, 1);
            }
        },1);

        if (spUtil.getInt(BaseApplication.MONEYKIND, 0) == 1)
        {
            homeSelectTwoPop.setKind(1);
            tv_2.setText("0.27¥/度");
            tv_22.setText("0.27¥/度");
        }
        else if(spUtil.getInt(BaseApplication.MONEYKIND, 0) == 2)
        {
            homeSelectTwoPop.setKind(2);
            tv_2.setText("0.040$/度");
            tv_22.setText("0.040$/度");
        }
        homeSelectThreePop = new HomeSelectThreePop(getActivity(), new HomeSelectThreePop.OnSelectItemListener() {
            @Override
            public void select(int i,String name) {

                if(name.equals("最赚钱"))
                {
                    pricekind=2;
                    sort(2);
                }
                else
                {
                    pricekind=1;
                    sort(1);
                }
                simpleAdapter.notifyDataSetChanged();
                smoothMoveToPosition(mRecyclerView, 0);
                tv_3.setText(name);
                tv_33.setText(name);
            }
        });
        /**
         * 第一个pop关闭时的监听
         */
        homeSelectOnePop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                selectStyle(1,false);
            }
        });
        /**
         * 第二个pop关闭时的监听
         */
        homeSelectTwoPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                selectStyle(2,false);
            }
        });
        /**
         * 第三个pop关闭时的监听
         */
        homeSelectThreePop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                selectStyle(3,false);
            }
        });
    }

    /**
     * 选择pop时的样式
     * @param n
     * @param b
     */
    private void selectStyle(int n,boolean b)
    {
        if(n==1)
        {

            if(b)
            {
                ly_top11.setBackgroundResource(R.drawable.lable_corner_bg);
                tv_1.setTextColor(getActivity().getResources().getColor(R.color.system_color));
                iv_1.setImageResource(R.mipmap.icon_up);
            }
            else
            {
                ly_top11.setBackgroundResource(R.drawable.lable_corners_bg);
                tv_1.setTextColor(getActivity().getResources().getColor(R.color.home_top_word));
                iv_1.setImageResource(R.mipmap.icon_down);

            }

        }

        else if(n==2)
        {

            if(b)
            {
                ly_top22.setBackgroundResource(R.drawable.lable_corner_bg);
                tv_2.setTextColor(getActivity().getResources().getColor(R.color.system_color));
                iv_2.setImageResource(R.mipmap.icon_up);
            }
            else
            {
                ly_top22.setBackgroundResource(R.drawable.lable_corners_bg);
                tv_2.setTextColor(getActivity().getResources().getColor(R.color.home_top_word));
                iv_2.setImageResource(R.mipmap.icon_down);

            }

        }

        else if(n==3)
        {
            if(b)
            {
                ly_top33.setBackgroundResource(R.drawable.lable_corner_bg);
                tv_3.setTextColor(getActivity().getResources().getColor(R.color.system_color));
                iv_3.setImageResource(R.mipmap.icon_up);
            }
            else
            {
                ly_top33.setBackgroundResource(R.drawable.lable_corners_bg);
                tv_3.setTextColor(getActivity().getResources().getColor(R.color.home_top_word));
                iv_3.setImageResource(R.mipmap.icon_down);

            }

        }
    }

    /**
     * 切换币种时的通知处理结果
     * @param str
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = false, priority = 0)
    public void handleData(String str) {
        if(str.equals("1"))//人民币
        {
            isfresh=true;
        }
        else //美元
        {

            isfresh=true;
        }


    }



    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(homeListBean==null)
        {
            return;
        }

        if(isfresh)//切换币种
        {
            if (spUtil.getInt(BaseApplication.MONEYKIND, 0) == 1)
            {
                flash_p="0.27";
//                simpleAdapter.setKind(1,Double.valueOf(flash_p));
//                simpleAdapter.notifyDataSetChanged();
                homeSelectTwoPop.setKind(1);
                kind=1;
                tv_2.setText("0.27¥/度");
                tv_22.setText("0.27¥/度");
//                getNetData(code);
                listData=homeListBean.getData().getMinerModelList();
                simpleAdapter.setNewData(listData);
                BaseApplication.Exchange=homeListBean.getData().getExchange();
                simpleAdapter.setExchange(homeListBean.getData().getExchange());
                simpleAdapter.setKind(kind,Double.valueOf(flash_p));
                sort(pricekind);
                simpleAdapter.notifyDataSetChanged();
            }
            else if(spUtil.getInt(BaseApplication.MONEYKIND, 0) == 2)
            {
                flash_p="0.040";
//                simpleAdapter.setKind(2,Double.valueOf(flash_p));
//                simpleAdapter.notifyDataSetChanged();
                homeSelectTwoPop.setKind(2);
                tv_2.setText("0.040$/度");
                tv_22.setText("0.040$/度");
                kind=2;
//                getNetData(code);
                listData=homeListBean.getData().getMinerModelList();
                simpleAdapter.setNewData(listData);
                BaseApplication.Exchange=homeListBean.getData().getExchange();
                simpleAdapter.setExchange(homeListBean.getData().getExchange());
                simpleAdapter.setKind(kind,Double.valueOf(flash_p));
                sort(pricekind);
                simpleAdapter.notifyDataSetChanged();
            }

            isfresh=false;

        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
